﻿package net.wangit.view.element.component;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.enumeration.EnumerationUtil;
import net.wangit.view.util.Html;

public class SelectElementWriter extends AbstractElementWriter {

	public SelectElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		MapList enumeration = (MapList) ec.getObject();
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		Html.set(html, EnumerationUtil.getEnumerationName(enumeration, value));
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeText(ac, html, ec, row);
		ElementUtil.setHidden(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<select name=\"").append(ElementUtil.getAlias(ec)).append(
				"\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E3");
		Html.setStyleWithWidth(html, ec.getWidth());
		Html.setSize(html, ec.getHeight());
		html.append(">");
		writeOptions(ac, html, ec, row);
		html.append("</select>");
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeDisabledControl(ac, html, ec, row);
		ElementUtil.setHidden(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<select");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E3D");
		Html.setStyleWithWidth(html, ec.getWidth());
		Html.setSize(html, ec.getHeight());
		html.append(" disabled>");
		writeOptions(ac, html, ec, row);
		html.append("</select>");
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		ElementUtil.setHidden(ac, html, ec, row);
	}

	private void writeOptions(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		MapList enumeration = (MapList) ec.getObject();
		if (!Checker.isEmpty(enumeration)) {
			String value = ElementUtil.getValueOrDefault(ac, ec, row);
			for (int i = 0; i < enumeration.size(); i++) {
				Row item = enumeration.getRow(i);
				String itemValue = item.get("value");
				String custom = item.get("custom");
				html.append("<option value=\"").append(itemValue).append("\"");
				Html.setSelected(html, value, itemValue);
				Html.setAttribute(html, custom);
				html.append(">").append(item.get("name")).append("</option>");
			}

		}
	}
}
